package com.nulldev.util.privacy;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import com.nulldev.util.io.FileUtil;
import com.nulldev.util.java.JVMUtil;
import com.nulldev.util.logging.LoggerFactory;
import com.nulldev.util.os.CPUUtil.CPU;
import com.nulldev.util.os.CPUUtil.Vendor;
import com.nulldev.util.os.GPUUtil.GPU;
import com.nulldev.util.other.ArrayUtils;
import com.nulldev.util.string.StringsUtil;

public class IDGenerator {
	
	private static final String[] VENDORS = {"Intel(tm)",
			"AMD(tm)"};
	private static final String[] GPU_VENDORS = {
			"AMD(tm)"
	};
	private static final String[] INTEL_CPUS;
	private static final String[] AMD_CPUS;
	
	private static final String[] AMD_GPUS;
	
	static {
		try {
			final String x = StringsUtil.limitSpaces(FileUtil.readFileToString(getResourceAsFile("cpus_intel.txt")), 1);
			final String[] xc = x.split(JVMUtil.lineSep());
			final ArrayList<String> out = new ArrayList<String>();
			for (final String xz : xc) {
				out.add(xz.substring(0, xz.indexOf(" - ")));
			}
			INTEL_CPUS = ArrayUtils.arrayListToArray(out);
		}catch (Exception e) {
			throw new RuntimeException(e);
		}
		try {
			final String x = StringsUtil.limitSpaces(FileUtil.readFileToString(getResourceAsFile("cpus_amd.txt")), 1);
			final String[] xc = x.split(JVMUtil.lineSep());
			final ArrayList<String> out = new ArrayList<String>();
			for (final String xz : xc) {
				out.add(xz.substring(0, xz.indexOf(" - ")));
			}
			AMD_CPUS = ArrayUtils.arrayListToArray(out);
		}catch (Exception e) {
			throw new RuntimeException(e);
		}
		try {
			final String x = StringsUtil.limitSpaces(FileUtil.readFileToString(getResourceAsFile("gpus_amd.txt")), 1);
			final String[] xc = x.split(JVMUtil.lineSep());
			final ArrayList<String> out = new ArrayList<String>();
			for (final String xz : xc) {
				out.add(xz.substring(0, xz.indexOf(" - ")));
			}
			AMD_GPUS = ArrayUtils.arrayListToArray(out);
		}catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public static void main(final String[] data) {
		System.out.println(generateCPU());
		System.out.println(generateGPU());
	}
	
	public static CPU generateCPU() {
		final String vendor = ArrayUtils.random(VENDORS);
		if (vendor.equals("Intel(tm)")) {
			final CPU cpu = new CPU(Vendor.INTEL, vendor + " " + ArrayUtils.random(INTEL_CPUS));
			if (cpu.getCPUData().contains("Core i") || cpu.getCPUData().contains("Core 2 ")) {
				cpu.override64(true);
			}
			return cpu;
		}else if (vendor.equals("AMD(tm)")) {
			final CPU cpu = new CPU(Vendor.AMD, vendor + " " + ArrayUtils.random(AMD_CPUS));
			if (cpu.getCPUData().contains("Phenom X4 ") || cpu.getCPUData().contains("FX ") || cpu.getCPUData().contains("Phenom II X")) {
				cpu.override64(true);
			}
			return cpu;
		}
		return null;
	}
	
	public static GPU generateGPU() {
		final String vendor = ArrayUtils.random(GPU_VENDORS);
		if (vendor.equals("AMD(tm)")) {
			final GPU gpu = new GPU(com.nulldev.util.os.GPUUtil.Vendor.AMD, ArrayUtils.random(AMD_GPUS));
			return gpu;
		}
		return null;
	}
	
	public static File getResourceAsFile(String resourcePath) {
	    try {
	        InputStream in = IDGenerator.class.getResourceAsStream(resourcePath);
	        if (in == null) {
	        	LoggerFactory.getLogger().error("getResourceAsFile(file) -> File not found! Returning null!");
	            return null;
	        }

	        File tempFile = File.createTempFile(String.valueOf(in.hashCode()), ".tmp");
	        tempFile.deleteOnExit();

	        final FileOutputStream out = new FileOutputStream(tempFile);
	        byte[] buffer = new byte[1024];
	        int bytesRead;
	        while ((bytesRead = in.read(buffer)) != -1) {
	            out.write(buffer, 0, bytesRead);
	        }
	        out.close();

	        return tempFile;
	    } catch (IOException e) {
	    	System.err.println("[FileUtil] Failed to read resource!");
	        e.printStackTrace();
	        return null;
	    }
	}
}
